package cn.itcast.servlet.online_test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.itcast.service.ChapterContentService;
import cn.itcast.service.StudentService;
import cn.itcast.service.TestsService;
import cn.itcast.service.impl.ChapterContentServiceImpl;
import cn.itcast.service.impl.StudentServiceImpl;
import cn.itcast.service.impl.TestsServiceImpl;
import cn.itcast.vo.ChapterContent;
import cn.itcast.vo.extend.TestsExtend;

public class FindTestsByTextBookId extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		TestsService testsService = new TestsServiceImpl();
		int sum = testsService.getChapterSum(Integer.parseInt(id));
		List<TestsExtend> list = testsService.getTestsByChapterId(sum);

		JSONObject obj = new JSONObject();
		try {
			obj.put("status", "success");
			JSONArray testss = new JSONArray();
			for (TestsExtend te : list) {
				JSONObject tests = new JSONObject();
				tests.put("id", te.getId());
				tests.put("testsContent", te.getTestsContent());
				tests.put("explain", te.getExplain());
				tests.put("sroce", te.getScore());
				tests.put("testsType", te.getName());
				tests.put("answer", te.getAnswer());
				JSONArray options = new JSONArray();
				JSONObject optionA = new JSONObject();
				optionA.put("A", te.getOptionA());
				JSONObject optionB = new JSONObject();
				optionB.put("B", te.getOptionB());
				JSONObject optionC = new JSONObject();
				optionC.put("C", te.getOptionC());
				JSONObject optionD = new JSONObject();
				optionD.put("D", te.getOptionD());
				options.put(optionA);
				options.put(optionB);
				options.put(optionC);
				options.put(optionD);

				tests.put("options", options);

				testss.put(tests);
			}
			obj.put("testss", testss);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String json = obj.toString();
		System.out.println(json);
		out.write(json);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String textBookId = request.getParameter("id");
		String tokenId = request.getParameter("token_id");

		StudentService studentService = new StudentServiceImpl();
		String token_id = studentService.getTokenid(tokenId);

		if (token_id == null) {
			JSONObject obj = new JSONObject();
			try {
				obj.put("status", "fail");
				String json = obj.toString();
				System.out.println(json);
				out.write(json);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			TestsService testsService = new TestsServiceImpl();
			int sum = testsService.getChapterSum(Integer.parseInt(textBookId));
			List<TestsExtend> list = testsService.getTestsByChapterId(sum);
			JSONObject obj = new JSONObject();
			try {
				obj.put("status", "success");
				JSONArray testss = new JSONArray();
				for (TestsExtend te : list) {
					JSONObject tests = new JSONObject();
					tests.put("id", te.getId());
					tests.put("testsContent", te.getTestsContent());
					tests.put("explain", te.getExplain());
					tests.put("sroce", te.getScore());
					tests.put("testsType", te.getName());
					tests.put("answer", te.getAnswer());
					JSONArray options = new JSONArray();
					JSONObject optionA = new JSONObject();
					optionA.put("A", te.getOptionA());
					JSONObject optionB = new JSONObject();
					optionB.put("B", te.getOptionB());
					JSONObject optionC = new JSONObject();
					optionC.put("C", te.getOptionC());
					JSONObject optionD = new JSONObject();
					optionD.put("D", te.getOptionD());
					options.put(optionA);
					options.put(optionB);
					options.put(optionC);
					options.put(optionD);

					tests.put("options", options);
					testss.put(tests);
				}
				obj.put("testss", testss);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String json = obj.toString();
			System.out.println(json);
			out.write(json);

		}
	}

}
